/**
 * 管理初始化
 */
var Column = {
    id: "ColumnTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Column.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: 'ID', field: 'columnId', visible: false, align: 'center', valign: 'middle'},
            {title: '栏目名称', field: 'columnName', visible: true, align: 'center', valign: 'middle'},
            {title: '创建人', field: 'createUser', visible: false, align: 'center', valign: 'middle'},
            {title: '创建时间', field: 'createDate', visible: false, align: 'center', valign: 'middle'},
            {title: '是否置顶', field: 'beTop', visible: true, align: 'center', valign: 'middle',formatter: function (value, row, index){
                var text = '置顶';
                if (value == "1") {
                    text = "置顶";
                } else if (value == "0") {
                    text = "不置顶";
                } 
                return text;
            }},
            {title: '状态', field: 'status', visible: true, align: 'center', valign: 'middle',formatter: function (value, row, index){
                var text = '下架';
                if (value == "1") {
                    text = "上架";
                } else if (value == "0") {
                    text = "下架";
                } 
                return text;
            }},
            {title: '站点', field: 'siteId', visible: true, align: 'center', valign: 'middle',formatter: function (value, row, index){
                var text = '';
                if (value == "001") {
                    text = "万柏林";
                } else if (value == "002") {
                    text = "运城";
                }else if (value == "003") {
                    text = "贵州";
                }else if (value == "004") {
                    text = "科协";
                }
                return text;
            }},
            {title: '栏目ID', field: 'columnCode', visible: false, align: 'center', valign: 'middle'},
            {title: '栏目描述', field: 'columnDesc', visible: false, align: 'center', valign: 'middle'},
            {title: '父id', field: 'pId', visible: false, align: 'center', valign: 'middle'},
            {title: '所有父ids', field: 'pIds', visible: false, align: 'center', valign: 'middle'},
            {title: '修改时间', field: 'updateDate', visible: false, align: 'center', valign: 'middle'},
            {title: '修改人', field: 'updateUser', visible: false, align: 'center', valign: 'middle'},
            {title: '排序', field: 'num', visible: false, align: 'center', valign: 'middle'},
            {title: '是否可删除', field: 'beEdit', visible: false, align: 'center', valign: 'middle',formatter: function (value, row, index){
                var text = '不可删除';
                if (value == "1") {
                    text = "可删除";
                } else if (value == "0") {
                    text = "不可删除";
                } 
                return text;
            }},
            { 
                field: 'operation', title: '操作',  width: '25%',align: 'center', valign: 'middle',
             formatter : function(cell, row, index) { 
                // btnEdit = '<a class="btn-warning btn-sm"   href="/testPlan/editSkip?id='+row.id+'">下架</a>';                  
                 //btnDetail = '<a class="btn-primary btn-sm"   href="/testPlan/getRecordById?id='+row.id+'">置顶</a>';                   
                 //btnDel = '<button type="button" class="btn btn-danger btn-xs" onclick="deleteCase('+row.id+')">删除</button>'; 
                // btnEdit = '<a class="btn-danger btn-sm"   href="/testPlan/editSkip?id='+row.id+'">删除</a>';     
                // btnStatistics= '<a class="btn-primary btn-sm" href="/testPlan/getTestPlanReport?id='+row.id+'">课程列表</a>'; 
                 //btnCaseAssociation= '<button type="button" class="btn-success  btn-xs" onclick="caseAssociation('+row.id+')">关联用例</button>'; 
            	 var DownOrUptext = '下架';
                 if (row.status == "1") {
                	 DownOrUptext = "下架";
                 } else if (row.status == "0") {
                	 DownOrUptext = "上架";
                 } 
                 
                 var TopOrNotext = '置顶';
                 if (row.beTop == "1") {
                	 TopOrNotext = "取消置顶";
                 } else if (row.beTop == "0") {
                	 TopOrNotext = "置顶";
                 }
                 
                 btnDownOrUp = '<button type="button" class="btn-warning btn-sm" onclick="Column.downOrUp('+row.columnId+')">'+DownOrUptext+'</button>'; 
                 btnTopOrNo = '<button type="button" class="btn-primary btn-sm" onclick="Column.topOrNo('+row.columnId+')">'+TopOrNotext+'</button>'; 
                 btnDel = '<button type="button" class="btn-danger btn-sm" onclick="Column.delete('+row.columnId+')">删除</button>'; 
                 btnRoomList = '<button type="button" class="btn-primary btn-sm" onclick="Column.roomList('+row.columnId+',\''+row.siteId+'\',\''+row.columnDesc+'\')">课程列表</button>'; 
                 cell =btnDownOrUp+btnTopOrNo+btnDel+btnRoomList; 
                 return cell; 
             } 
            }
           
    ];
};

/**
 * 检查是否选中
 */
Column.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Column.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加
 */
Column.openAddColumn = function () {
    var index = layer.open({
        type: 2,
        title: '添加',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/column/column_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看详情
 */
Column.openColumnDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/column/column_update/' + Column.seItem.columnId
        });
        this.layerIndex = index;
    }
};

/**
 * 删除
 */
Column.delete = function (id) {
	var operation = function(){
        var ajax = new $ax(Feng.ctxPath + "/column/delete", function (data) {
            Feng.success(data.message);
            Column.table.refresh();
        }, function (data) {
        	console.log(data)
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("columnId",id);
        ajax.start();
	};
	Feng.confirm("是否删除?",operation);
};

/**
 * 置顶
 */
Column.topOrNo = function (id) {
        var ajax = new $ax(Feng.ctxPath + "/column/topOrNo", function (data) {
            Feng.success("操作成功!");
            Column.table.refresh();
        }, function (data) {
            Feng.error("操作失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id",id);
        ajax.start();
    
};

/**
 * 上下架
 */
Column.downOrUp = function (id) {
   
        var ajax = new $ax(Feng.ctxPath + "/column/downOrUp", function (data) {
            Feng.success("操作成功!");
            Column.table.refresh();
        }, function (data) {
            Feng.error("操作失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id",id);
        ajax.start();
  
};

Column.roomList = function (columnId,siteId,columnDesc) {
    var index = layer.open({
        type: 2,
        title: '课程列表',
        area: ['1000px', '450px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/column/toColumnRoomList?columnId='+columnId+'&siteId='+siteId+"&columnDesc="+columnDesc
    });
    this.layerIndex = index;
};

/**
 * 查询列表
 */
Column.search = function () {
    var queryData = {};
    queryData['columnName'] = $("#columnName").val();
    queryData['siteId'] = $("#siteId").val();
    queryData['beEdit'] = $("#beEdit").val();
    queryData['beDelete'] = $("#beDelete").val();
    Column.table.refresh({query: queryData});
};

/**
 * 重置
 */
Column.resetSearch = function () {
    $("#columnName").val("");
    $("#siteId").val("");
    $("#beEdit").val("");
    $("#beDelete").val("");
    Column.search();
}



$(function () {
    var defaultColunms = Column.initColumn();
    var table = new BSTable(Column.id, "/column/list", defaultColunms);
    table.setPaginationType("client");
    Column.table = table.init();
});
